package com.lu.service;

import com.lu.pojo.Url;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

@Service
public class UploadServiceImpl implements UploadService{
    @Override
    public String uploadFile(Url url,String uimg) throws IOException {
        base64ToFile(url.getUrl(),uimg);
        return null;
    }

    public static void base64ToFile(String base64,String uimg) throws IOException {
        byte[] bs = new byte[1024];
        bs = Base64.getMimeDecoder().decode(base64);
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        file = new File("/www/wwwroot/http/icon/"+uimg+".jpg");
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
